import { ChangeDetectorRef, Component, Input, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { UtilService } from '@core/services/util.service';

@Component({
  selector: 'app-qrcode-add',
  templateUrl: './qrcode-add.component.html',
  styles: []
})
export class QrcodeAddComponent implements OnInit {
  @ViewChild('treedom',{ static: false }) treedom:any;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @Input()type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    console.log(this.param);
    
  };
  param: any;
  value:any;
  nodes=[];
  timeout:any="";
  show:boolean=true;
  showForm = false;
  isLoading:boolean=false;
  schema: SFSchema = {
    properties: {
      qrCode: {
        type: 'number',
        title: '选择位置',
        enum: [
          {label: '负一层', value: 1},
          {label: '负二层', value: 2},
          {label: '负三层', value: 3}
        ],
        ui: {
          widget: 'select',
          hidden:true,
          change:(res)=>{
            console.log(res)
          }
        }
      },
      upgradeName: {
        type: 'string',
        title: '具体位置',
        ui:{
          hidden:false
        }
      },

    },
    required: ['upgradeName','qrCode']
  }

  formSubmit(data) {
    let fileUrl = '';
   
    const body: any = {
      upgradeWay:data.upgradeWay,
      upgradeName: data.upgradeName,
      upgradeSize: data.upgradeSize,
      upgradeFacCode: data.upgradeFacCode,
      upgradeType:data.upgradeType,
      upgradeUrl:fileUrl,
      upgradeFeature:data.upgradeFeature,
      upgradeVersion:data.upgradeVersion,
      upgradeVersionStatus:data.upgradeVersionStatus
    }
    const url = '/core/upgrade';
    const method = 'POST';
    this.http.request<any>(method, url,  { body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success('操作成功');
        this.modalRef.destroy('ok');
      } else {
        this.msg.error(res.message)
      }
    })

  }
  debounce(fn,wait,data){
    const self=this
    return ()=>{
      if(self.timeout) clearTimeout(self.timeout)
      self.timeout=setTimeout(()=>{
        fn.call(self,data)
        this.isLoading=true
      },wait)
    }
  }
  onKey(event){
    let fn=this.debounce(this.getHouses,500,event.target.value)
    fn()
  }
//点击下拉菜单节点
onChange($event: string): void {
  let data=this.treedom.getTreeNodeByKey($event);
  console.log(data)
  let id=data.origin.id;
  //如果选择的楼栋单元下有设备的描述，则显示选择描述的下拉框，否则不显示下拉框，由用户手动输入
  this.schema.properties["qrCode"].ui["hidden"] = false
  this.schema.properties["upgradeName"].ui["hidden"] = true
  this.sf.refreshSchema()
}

    //获取楼栋房间列表
    getHouses(val): void{
      this.http.post(this.api.manager.queryUnitHouse.url,{"areaParentAllName":val}).subscribe( (res:any) => {
        console.log(res);
        if (res.status === '200' && res.data && res.data.list.length > 0) {
          let list=[];
          for(let item of res.data.list){
            list.push({
              key:item.id,
              pId:item.areaId == undefined ? "" : item.areaId,
              title:item.areaParentAllName,
              id:item.id,
              value:item.id
            })
          }
          // console.log(list);
          this.nodes=this.util.getTreeNodeOption(list);
          document.getElementById('tree').click()
          setTimeout(() => {
            document.getElementById('tree').click()
          }, 50);
          this.isLoading = false
        }
      })
    }

  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private util: UtilService,
    private ref: ChangeDetectorRef,
  ) { }

  async ngOnInit() {
    this.showForm = true;
  }

}
